﻿using MultiChoiceVoting.Helpers;
using MultiChoiceVoting.Models;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using System;

namespace MultiChoiceVoting.Drivers
{
    public class MultiChoiceVotingPartDriver : ContentPartDriver<MultiChoiceVotingPart>
    {
        protected override string Prefix
        {
            get
            {
                return "MultiChoiceVoting";
            }
        }

        protected override DriverResult Display(MultiChoiceVotingPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_MultiChoiceVoting",
                () => shapeHelper.Parts_MultiChoiceVoting());
        }

        protected override DriverResult Editor(MultiChoiceVotingPart part, dynamic shapeHelper)
        {
            return ContentShape("Parts_MultiChoiceVoting_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts.MultiChoiceVoting",
                    Model: part,
                    Prefix: Prefix
                    ));
        }

        protected override DriverResult Editor(MultiChoiceVotingPart part, IUpdateModel updater, dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);

            part.SerializedAnswers = AnswerSerializerHelper.SerializeAnswerList(part.AnswerList);

            return Editor(part, shapeHelper);
        }
    }
}